<?php
/**
 * @文件功能名称  :
 * @Author      : yaoGrace
 * @Date        : 2021/12/9 21:19
 */
namespace phpGrace\models;
class managerole extends  \graceModel{
    // 表名
    public $tableName ="manager_roles";
    //主键
    public  $tableKey ="role_id";

    /**
     * 添加数据
     * @param $data
     * @return 写入数据对应的主键数据值
     */
    public function add($data){
        return $this->m->add($data);
    }

    /**
     * 修改数据
     * @param $id
     * @param $data
     */
    public function update($id,$data){
      return  $this->m->where("role_id = ? ",array($id))->update($data);
    }

    /**
     * 删除
     * @param $id
     * @return bool
     */
    public function delete($id){
        return $this->m->where("role_id = ? ",array($id))->delete();
    }
    /**
     * 获取某一条数据
     * @param $id
     * @return array
     */
    public function getFetchByID($id){
        //intval() 函数用于获取变量的整数值
        return $this->m->where($this->tableKey .' = ?', array(intval($id)))->fetch();
    }

    //查询所有数据
    public function __getAllListReturnJson(){
        $page  = isset($_GET['page'])  ?  $_GET['page']   : 1;
        $limit = isset($_GET['limit']) ?  $_GET['limit']  : 10;
        $total = $this->m->count();
        if($total>0){
            if ($page > 0 && $page < (ceil($total / $limit) + 1)) {
                $pagenum = ($page - 1) * $limit;
            } else {
                $pagenum = 0;
            }
            $items = $this->m->order('role_id asc')->limit($pagenum,$limit)->fetchAll();;
            if (empty($items)) {
                $data = array('code'=>1002,'count'=>0,'data'=>array(),'msg'=>'无数据');
            } else {
                $data = array('code'=>0,'count'=>$total,'data'=>$items,'msg'=>'有数据');
            }
        }else{
            $data = array(
                'code'     =>  1001,
                'count'    =>  0   ,
                'msg'      =>  '无数据'
            );
        }
        return json_encode($data);
    }

    //获取所有权限
    public function getAllRoles(){
        return $this->m->order($this->tableKey." asc")->fetchAll();
    }

    public function getRoleByPage(){
       return $this->m->order("role_id asc")->page(10)->fetchAll();
    }
}